﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using ComicIsland.App.Resources;
using ComicIsland.Common;
using ComicIsland.Common.Entities;

/**
 * 创建时间：2014/12/25 14:39:25
 * 作者：yong.blackcore
 * Email：itblackhole@outlook.com
 * QQ：50404503
 */
namespace ComicIsland.App.DownloadConverter
{
    /// <summary>
    /// 按钮状态
    /// </summary>
    public class ComicChapterOperationConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string result = AppResources.DownQueueStatusWaitDown;
            try
            {
                string comicAndPartId = string.Empty;
                if (value != null)
                {
                    comicAndPartId = value.ToString();
                }

                if (!string.IsNullOrWhiteSpace(comicAndPartId))
                {
                    var cpIds = comicAndPartId.Split('#');
                    if (cpIds != null && cpIds.Length == 2)
                    {
                        int comicId = 0, partId = 0;
                        int.TryParse(cpIds[0], out comicId);
                        int.TryParse(cpIds[1], out partId);

                        var items = App.comicDbHelper.ImageGetByComicIdAndChapterId(comicId, partId);
                        if (items != null && items.Count > 0)
                        {
                            int completedTotal = items.Where(p => DownloadComicChapterStatus.Completed.ToString().Equals(p.DownloadStatus)).Count();
                            if (items.Count == completedTotal)
                                result = AppResources.DownQueueStatusCompleted;
                            else
                                result = AppResources.DownQueueStatusDownloading;
                        }                    
                    }
                }
            }
            catch (Exception err)
            {
                LogHelper.LogError(err.Message, this.GetType(), MethodBase.GetCurrentMethod());
            }
            string operationResult = result;
            if (AppResources.DownQueueStatusCompleted.Equals(operationResult))
            { 
                operationResult= AppResources.DownQueueOperateRead;
            }
            else if (AppResources.DownQueueStatusPause.Equals(operationResult))
            {
                operationResult = AppResources.DownQueueOperateDown;
            }
            else if (AppResources.DownQueueStatusDownloading.Equals(operationResult))
            {
                operationResult = AppResources.DownQueueOperatePause;
            }
            else if (AppResources.DownQueueStatusWaitDown.Equals(operationResult))
            {
                operationResult = AppResources.DownQueueOperateDelete;
            }
            return operationResult;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return null;
        }
    }
}
